package com.sskj.phoenixcoin.activity;


import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.github.tifezh.kchartlib.chart.formatter.TimeFormatter;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.phoenixcoin.R;
import com.sskj.phoenixcoin.bean.AppRecord;
import com.sskj.phoenixcoin.component.DaggerUserDataComponent;
import com.sskj.phoenixcoin.presenter.AppRecordActivityPresenter;
import com.sskj.lib.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;


public class AppRecordActivity extends BaseActivity<AppRecordActivityPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter<AppRecord.Record> slimAdapter;
    private MVCCoolHelper<List<AppRecord.Record>> mvcCoolHelper;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private boolean isWithdraw;   // true 提现

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_record;
    }

    @Override
    public AppRecordActivityPresenter getPresenter() {
        return new AppRecordActivityPresenter();
    }

    public static void start(Context context, boolean isWithdraw) {
        Intent intent = new Intent(context, AppRecordActivity.class);
        intent.putExtra("isWithdraw", isWithdraw);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        super.initView();
        isWithdraw = getIntent().getBooleanExtra("isWithdraw", false);
        tvTitle.setText(isWithdraw ? "提现记录" : "充值记录");
        ClickUtil.click(ivBack, () -> {
            finish();
        });
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                initAdapter();
            } else {
                userData = null;
            }
        });
    }

    protected void initAdapter() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 10))
                .setLastDraw(false)
                .setFirstDraw(false)
                .setDividerColor(ContextCompat.getColor(this, R.color.app_040)));
        slimAdapter = SlimAdapter.create().register(R.layout.app_record_item, new SlimInjector<AppRecord.Record>() {

            @Override
            public void onInject(AppRecord.Record data, IViewInjector injector, List<Object> payloads) {
                injector.text(R.id.date, TimeFormatUtil.SF_FORMAT_E.format(Double.valueOf(data.getCreateTime())))
                        .text(R.id.status, data.getInspectStatus().equals("0") ? "待审核" : data.getInspectStatus().equals("1") ? "已审核" : "已拒绝")  //充币记录 0:待审核 1:已审核 2:拒绝 3:审核中
                        .text(R.id.num, NumberUtil.keep2(data.getCoinNum()) + "FHB")
                        .text(R.id.date_type, isWithdraw ? "提现时间" : "充值时间")
                        .text(R.id.num_type, isWithdraw ? "提现数量" : "充值数量")
                        .text(R.id.money, NumberUtil.keep2(data.getFee())+"CNY");
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<AppRecord.Record> dataSource =
                new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<AppRecord.Record>)
                        page -> {
                            return mPresenter.getRecord(page, Constans.PAGE_SIZE, String.valueOf(userData.getId()), isWithdraw ? 2 : 1);
                        }, Constans.PAGE_SIZE);
        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcCoolHelper.setDataSource(dataSource);
        mvcCoolHelper.setAdapter(slimAdapter);
        mvcCoolHelper.refresh();
    }
}
